package mylittleinvestigations;

import mylittleinvestigations.CaseInformation.CaseXmlWriter;
import mylittleinvestigations.CaseInformation.CaseXmlReader;
import java.io.IOException;

public class Vector2
{
    private double x;
    private double y;
    
    public double getX() { return this.x; }
    public void setX(double x) { this.x = x; }
    public double getY() { return this.y; }
    public void setY(double y) { this.y = y; }
    
    public Vector2()
    {
        this(0, 0);
    }
    
    public Vector2(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
    
    public double length()
    {
        return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY());
    }
    
    public Vector2 normalize()
    {
        return new Vector2(this.getX() / this.length(), this.getY() / this.length());
    }
    
    public Vector2 add(Vector2 operand)
    {
        return new Vector2(this.getX() + operand.getX(), this.getY() + operand.getY());
    }
    
    public Vector2 subtract(Vector2 operand)
    {
        return new Vector2(this.getX() - operand.getX(), this.getY() - operand.getY());
    }
    
    public Vector2 rotate(double radians)
    {
        return new Vector2(
                this.getX() * Math.cos(radians) - this.getY() * Math.sin(radians),
                this.getX() * Math.sin(radians) + this.getY() * Math.cos(radians));
    }
    
    public Vector2 scalarMultiply(double scalar)
    {
        return new Vector2(this.getX() * scalar, this.getY() * scalar);
    }
    
    public double dot(Vector2 operand)
    {
        return this.getX() * operand.getX() + this.getY() * operand.getY();
    }
    
    public Vector2 Clone()
    {
        return new Vector2(this.x, this.y);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (!(obj instanceof Vector2))
        {
            return false;
        }
                
        Vector2 vector = (Vector2)obj;

        if (vector == null)
        {
            return false;
        }

        return this.getX() == vector.getX() && this.getY() == vector.getY();
    }

    @Override
    public int hashCode()
    {
        return (int)(7 * Double.doubleToLongBits(this.getX()) + Double.doubleToLongBits(this.getY()));
    }
    
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        sb.append(this.getX());
        sb.append(", ");
        sb.append(this.getY());
        sb.append(")");
        
        return sb.toString();
    }

    public void SaveToXml(CaseXmlWriter writer) throws IOException
    {
        writer.StartElement("Vector2");
        writer.WriteDoubleElement("X", this.x);
        writer.WriteDoubleElement("Y", this.y);
        writer.EndElement();
    }
    
    public Vector2(CaseXmlReader reader)
    {
        reader.StartElement("Vector2");
        this.x = reader.ReadDoubleElement("X");
        this.y = reader.ReadDoubleElement("Y");
        reader.EndElement();
    }
}
